package com.ehotting.edsta.diagnosisPredict.controller;

import com.ehotting.eaf.core.domain.R;
import com.ehotting.eaf.core.utils.DateUtils;
import com.ehotting.eaf.core.web.domain.diagnosisPredict.PageDto;
import com.ehotting.eaf.security.service.TokenService;
import com.ehotting.edsta.diagnosisPredict.domain.dto.MaterialInfoDto;
import com.ehotting.edsta.diagnosisPredict.domain.dto.MaterialInfoSearchDto;
import com.ehotting.edsta.diagnosisPredict.feign.MaterialFeignService;
import com.ehotting.edsta.diagnosisPredict.utils.CommonUtils;
import feign.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.List;

/**
 * 素材集
 */
@Slf4j
@RestController
@RequestMapping("/material")
@Api(tags = "MaterialController",description = "素材集接口")
public class MaterialController{

    @Autowired
    private TokenService tokenService;

    @Autowired
    private MaterialFeignService materialFeignService;

    /**
     * 填写信息
     * @return
     */
    @ApiOperation(value = "填写信息",notes = "返回的数据类型：PageDto\\<MaterialInfoDto\\>")
    @PostMapping(value = "/search")
    public R<PageDto<MaterialInfoDto>> materialInfoSearch(@RequestBody MaterialInfoSearchDto dto){
        return R.convert(materialFeignService.materialInfoSearch(dto));
    }

    /**
     * 文件上传保存
     * @return
     */
    @ApiOperation(value = "文件上传保存",notes = "返回的数据类型：MaterialInfoDto")
    @PostMapping(value = "/save-and-upload")
    public R<MaterialInfoDto> saveAndUpload(@RequestParam("file") MultipartFile file,
                                            @RequestParam("fileDesc") String fileDesc,
                                            @RequestParam("fileName") String fileName,
                                            @RequestParam("projectId") String projectId){
        return R.convert(materialFeignService.saveAndUpload(file, fileDesc, fileName,projectId));
    }

    /**
     * 我的数据库选择
     * @return
     */
    @ApiOperation(value = "我的数据库选择",notes = "返回的数据类型：MaterialInfoDto")
    @PostMapping(value = "/save")
    public R<MaterialInfoDto> materialInfoSave(@RequestBody MaterialInfoDto dto){
        return R.convert(materialFeignService.materialInfoSave(dto));
    }

    /**
     * 数据预览
     * @param fileId
     * @return
     */
    @ApiOperation(value = "数据预览",notes = "返回的数据类型：List\\<String[]\\>")
    @GetMapping(value = "/viewfile")
    public R<List<String[]>> viewfile(@RequestParam("encoding") String encoding, Long fileId){
        return R.convert(materialFeignService.viewfile(encoding, fileId));
    }

    /**
     * 删除（批量）
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除（批量）")
    @GetMapping(value = "/batch")
    public R<Boolean> batch(@RequestParam("idList") String ids) {
        return R.convert(materialFeignService.batch(ids));
    }

    /**
     * 下载数据集
     * @param fileId
     * @return
     */
    @PostMapping(value = "/downloadfile")
    @ApiOperation("下载数据集")
    public void downloadfile(@RequestParam(value = "fileId") Long fileId, HttpServletResponse response) {
        // 构建默认的文件名
        String filename = DateUtils.dateTimeNow() + ".xlsx";
        feign.Response downloadfile = materialFeignService.downloadfile(fileId);
        CommonUtils.downloadFileFromFeign(response, filename, downloadfile);
    }

    /**
     * 创建数据集
     * @return
     */
    @PostMapping(value = "/save-no-project")
    @ApiOperation("创建数据集")
    public R<MaterialInfoDto> saveNoProject(@RequestBody MaterialInfoDto dto) {
        return R.convert(materialFeignService.saveNoProject(dto));
    }

    /**
     * 材料文件上传
     * @return
     */
    @PostMapping(value = "/upload-no-project/{id}")
    @ApiOperation("材料文件上传")
    public R<MaterialInfoDto> uploadNoProject(@RequestParam("file") MultipartFile file, @PathVariable("id") Long id) {
        return R.convert(materialFeignService.uploadNoProject(file,id));
    }

    /**
     * 下载模板（会存在txt,csv,xls,xlsx,zip的类型）
     * @param projectType
     * @return
     */
    @GetMapping(value = "/getUrl")
    @ApiOperation("下载模板")
    public void getUrl(@RequestParam("projectType ") String projectType,HttpServletResponse response){
        Response url = materialFeignService.getUrl(projectType);
        CommonUtils.downloadFileFromFeign(response,"",url);
    }

}